<?php

namespace model;

class UserCredentials {

	//private $m_validUsernames = "Admin" ;
    //private $m_validPasswords = "Password";
    private $m_hasCorrectUsername = false;
    private $m_hasCorrectPassword = false;
    private $m_correctUsername = "";
    private $m_correctPassword = "";



	//Här sätter vi rätt systemstatus och kontrollerar anändaruppgifterna
	public function checkUserCredentials($userUsername, $userPassword)	{

        //var_dump($this->m_correctUsername);

        $username ="";
        $password ="";


        $connect = mysql_connect('195.128.174.38', 'wse336700', '8nq8cf4963');
        if (!$connect) {
            die('Could not connect: ' . mysql_error());
        }

        mysql_select_db('wse336700_1', $connect) or die('Could not select database.');
        $result = mysql_query("SELECT * FROM users WHERE username LIKE '". $userUsername."'");
        mysql_close($connect);
        $num_rows = mysql_num_rows($result);

        if ( $num_rows > 0) {

            while($row = mysql_fetch_array($result)) {
                $username =$row[1];
                $password =$row[2];
            }

         if($userUsername == $username) {
                  $this->m_hasCorrectUsername = true;
                  $this->m_correctUsername = $userUsername;

                  if($userPassword == $password) {
                      $this->m_hasCorrectPassword = true;
                      $this->m_correctPassword = $userPassword;
                  }

           }

        }


        if($this->m_hasCorrectUsername == true && $this->m_hasCorrectPassword == true) {
           return true;
        } else {
            return false;
        }
	}



    public function addUserToDatabase($userName , $password)     {

        $connect = mysql_connect('195.128.174.38', 'wse336700', '8nq8cf4963');
        if (!$connect) {
            die('Could not connect: ' . mysql_error());
        }

        mysql_select_db('wse336700_1', $connect) or die('Could not select database.');
        mysql_query("INSERT INTO wse336700_1.users (username, password) VALUES ('". $userName ."', '". $password. "')");

        mysql_close($connect);
        //$_SESSION['registeredUserOk'] = true;

    }




    public function isUserNameTaken($userUsername)  {

        $connect = mysql_connect('195.128.174.38', 'wse336700', '8nq8cf4963');
        if (!$connect) {
            die('Could not connect: ' . mysql_error());
        }

        mysql_select_db('wse336700_1', $connect) or die('Could not select database.');
        $result = mysql_query("SELECT * FROM users WHERE username LIKE '". $userUsername."'");
        $num_rows = mysql_num_rows($result);

        if ( $num_rows > 0) {
            return true;
            } else {
                return false;
        }

    }









    public function hasValidUsername() {
        return $this->m_hasCorrectUsername;
    }

    public function hasValidPassword() {
        return $this->m_hasCorrectPassword;
    }

    public function returnValidUsername() {
        return $this->m_correctUsername;
    }

    public function returnValidPassword() {
        return $this->m_correctPassword;
    }




}


